/*****************************
 *@file: SplitBulletComp
 *@author: 陈吕唐
 *@desc: 分裂子弹
 *@date: 2024-03-12	10:25
 *****************************/
import { Collider2D, IPhysics2DContact, Node, _decorator, math, misc, v2, v3 } from 'cc';
import GameSingle from '../GameSingle';
import GameUtils from '../GameUtils';
import { PhysicsGroupEnum } from '../enum/PhysicsGroupEnum';
import { BaseBulletComp } from './BaseBulletComp';
const { ccclass, property } = _decorator;

@ccclass('SplitBulletComp')
export class SplitBulletComp extends BaseBulletComp {
    /****************************************************************************************************************
     *Readonly 常量
     ****************************************************************************************************************/
    /****************************************************************************************************************
     *Property 定义变量
     ****************************************************************************************************************/
    /**
     * 分裂数量
     */
    private number: number = 3;
    /***************************************************************
     *PropertyUI 引用的控件
     ***************************************************************/
    /****************************************************************************************************************
     * Lifecycle 生命周期相关方法
     ****************************************************************************************************************/
    /****************************************************************************************************************
     * Public 公有方法
     ****************************************************************************************************************/
    /****************************************************************************************************************
     * private 私有方法
     *************************************************************·***************************************************/
    protected override onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact): void {
        super.onBeginContact(selfCollider, otherCollider, contact);

        switch (otherCollider.group) {
            case PhysicsGroupEnum.BORDER:
                this.recycleBullet();
                break;
            default:
                break;
        }


        let fight = GameUtils.getFightByCollider(otherCollider);

        if (!fight) {
            return;
        }

        contact.disabled = true;
        fight.hurt(this);
    }

    protected override onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact): void {
        super.onEndContact(selfCollider, otherCollider, contact);
        this.spiltBullet();
        this.recycleBullet();
    }

    /**
     * 分裂弹逻辑
     */
    private spiltBullet() {
        /**
         * 分裂数量
         */
        let splitNumber = this.number;

        /**
         * 分裂后速度向量
         */
        let vs: math.Vec2[] = [];
        let origin = this.body.linearVelocity;
        /**
         * 角度范围
         */
        let angleRadius = 60;
        let angleInterval = angleRadius / (this.number - 1);
        for (let i = 0; i < splitNumber; i++) {
            let angle = i * angleInterval - angleRadius / 2;
            let v = this.rotateVector(origin, angle);
            vs.push(v);
        }

        /**
         * 创建分裂后子弹节点
         */
        let splitNodes: Node[] = [];


        for (let i = 0; i < splitNumber; i++) {
            //FIXME:这里使用对象池问题 复用时候下次发送的就是上一个的子弹的分裂后的
            let node = GameSingle.single.createBulletNode(this, 1);
            node.position = v3(this.node.position.x, this.node.position.y, 0);
            let comp = node.getComponent(BaseBulletComp);
            let v = vs[i];
            comp.setLinearVelocity(v);
            splitNodes.push(node);
        }

        this.scheduleOnce(() => {
            for (let i = 0; i < splitNodes.length; i++) {
                const add = splitNodes[i];
                this.node.parent.addChild(add);
            }
        })
    }

    /**
     * 
     * @param vec 原有速度向量
     * @param angle 偏转角度
     * @returns 
     */
    private rotateVector(vec: math.Vec2, angle: number) {
        let radians = misc.degreesToRadians(angle);
        let newX = vec.x * Math.cos(radians) - vec.y * Math.sin(radians);
        let newY = vec.x * Math.sin(radians) + vec.y * Math.cos(radians);
        return v2(newX, newY);
    }
    /****************************************************************************************************************
     * Property Get && Set 属性的 get && set 函数
     ****************************************************************************************************************/

}


